package com.focus.wecenter.util;

import java.util.List;

/**
 * 分页的JavaBean
 * @author Administrator
 */
public class PageBean<T> {

    /**当前页*/
	private int currentpage;
    /**总页数*/
	private int totalpage;
    /**总记录数*/
	private int totalcount;
    /**每页显示的记录条数*/
	private int pagesize;
    /**每页显示的数据*/
	private List<T> list;



	public int getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	/**
	 * 调用getTotalPage() 获取到总页数
	 * JavaBean的属性规定：totalPage是JavaBean是属性 ${pageBean.totalPage}
	 */
	public int getTotalpage() {
		// 计算
		int totalPage = totalcount / pagesize;
		// 说明整除
		if(totalcount % pagesize == 0){
			return totalPage;
		}else{
			return totalPage + 1;
		}
	}
	public int getTotalcount() {
		return totalcount;
	}
	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}

	public int getPreviousPage() {
	    int page = currentpage - 1;
	    return page < 1? 1 : page;
    }
    public int getNextPage() {
	    int page = currentpage + 1;
	    return page > this.getTotalpage()? this.getTotalpage() : page;
    }
	
    public int[] getPageBar() {
        int[] bars;
        int startPage;
        int endPage;
        int totalPage = this.getTotalpage();
        int currentPage = this.getCurrentpage();

        if (totalPage <= 8) {
            startPage = 1;
            endPage = currentPage + 3;
            if (endPage > totalPage) {
                endPage = totalPage;
            }
        } else {
            startPage = currentPage - 3;
            endPage = currentPage + 3;
            if (startPage < 1) {
                startPage = 1;
            }
            if (endPage > totalPage) {
                endPage = totalPage;
            }
        }
        bars = new int[endPage - startPage + 1];
        int index = 0;
        for (int i = startPage; i <= endPage; i++) {
            bars[index] = i;
            index++;
        }
        return bars;
    }
}
